﻿using AutoMapper;
using BW.MesProject.Public.ErrorCode;
using BW.MesProject.Public.Infrastructure;
using BW.MesProject.WareHouse.Domain;
using BW.MesProject.WareHouse.Infrastructure;
using BW.MesProject.WareHouse.Infrastructure.Dto.Input;
using BW.MesProject.WareHouse.Infrastructure.Dto.OutPut;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BW.MesProject.WareHouse.Application
{
    /// <summary>
    /// 部门信息
    /// </summary>
    public class DepServices : IDepServices
    {
        private readonly IBaseRepository<DEPModel> _Dep;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="Dep"></param>
        /// <param name="mapper"></param>
        public DepServices(IBaseRepository<DEPModel> Dep, IMapper mapper)
        {
            _Dep = Dep;
            this._mapper = mapper;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Dep"></param>
        /// <returns></returns>
        public ApiResult<int> AddDep(DEPModel Dep)
        {
            try
            {
                //判断部门编码，名称是否为空
                if (string.IsNullOrEmpty(Dep.DepId) || string.IsNullOrEmpty(Dep.DepName))
                {
                    return new ApiResult<int>()
                    {
                        Code = ApiEnums.Fail,
                        Message = "请检查部门编码，名称是否为空"
                    };
                }
                var model = _Dep.GetValue(x => x.DepId == Dep.DepId);
                if (model != null && !string.IsNullOrEmpty(model.DepId))
                {
                    return new ApiResult<int>()
                    {
                        Code = ApiEnums.Fail,
                        Message = "部门编号重复，请重新检查修改！！！"
                    };
                }
                model = _Dep.GetValue(x => x.DepName == Dep.DepName);
                if (model != null && !string.IsNullOrEmpty(model.DepName))
                {
                    return new ApiResult<int>()
                    {
                        Code = ApiEnums.Fail,
                        Message = "部门名称重复，请重新检查修改！！！"
                    };
                }

                //添加
                var models = _mapper.Map<DEPModel>(Dep);

                return new ApiResult<int>
                {
                    Code = ApiEnums.Success,
                    Message = "添加成功",
                    Data = _Dep.Add(Dep),
                    TotalCount = 0
                };
            }
            catch (Exception ex)
            {
                // 记录异常信息
                Console.WriteLine($"添加部门时发生异常: {ex.Message}");

                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Message = $"添加失败: {ex.Message}",
                    Data = 0,
                    TotalCount = 0
                };
            }
        }


        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="qryInput"></param>
        /// <returns></returns>
        public ApiResult<List<DEPModel>> GetDep(QryInput qryInput)
        {
            try
            {
                int totalCount = 0;

                // 获取所有库位
                var list = _Dep.GetAll().Where(x => x.IsDeleted == false);


                if (qryInput.PageSize == 0)
                {
                    list = list.OrderBy(u => u.CreatedTime).Where(x => x.IsEnable == true);
                }
                else
                {
                    // 按编码查询
                    if (!string.IsNullOrEmpty(qryInput.QryID) && !string.IsNullOrEmpty(qryInput.QryName))
                    {
                        list = list.Where(x => x.DepId.Contains(qryInput.QryID) && x.DepName.Contains(qryInput.QryName));
                    }
                    else if (!string.IsNullOrEmpty(qryInput.QryID))
                    {
                        list = list.Where(x => x.DepId.Contains(qryInput.QryID));
                    }
                    else if (!string.IsNullOrEmpty(qryInput.QryName))
                    {
                        list = list.Where(x => x.DepName.Contains(qryInput.QryName));
                    }


                }
                return new ApiResult<List<DEPModel>>()
                {
                    Code = ApiEnums.Success,
                    Message = "查询成功",
                    Data = list.ToList(),
                    TotalCount = list.ToList().Count(),
                    PageNum = qryInput.PageNum,
                    PageSize = qryInput.PageSize
                };

            }
            catch (Exception ex)
            {
                // 记录异常信息
                Console.WriteLine($"查询部门时发生异常: {ex.Message}");

                return new ApiResult<List<DEPModel>>()
                {
                    Code = ApiEnums.Fail,
                    Message = $"查询失败: {ex.Message}",
                    Data = new List<DEPModel>(),
                    TotalCount = 0
                };
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="DepId"></param>
        /// <returns></returns>
        public ApiResult<DEPModel> GetDepBYId(string DepId)
        {
            try
            {

                // 获取所有库位
                var list = _Dep.GetAll().Where(x => x.IsDeleted == false);

                // 按编码查询
                if (!string.IsNullOrEmpty(DepId) )
                {
                    list = list.Where(x => x.DepId== DepId);
                }
                return new ApiResult<DEPModel>()
                {
                    Code = ApiEnums.Success,
                    Message = "查询成功",
                    Data = list.First()
                };

            }
            catch (Exception ex)
            {
                // 记录异常信息
                Console.WriteLine($"查询部门时发生异常: {ex.Message}");

                return new ApiResult<DEPModel>()
                {
                    Code = ApiEnums.Fail,
                    Message = $"查询失败: {ex.Message}",
                    Data = new DEPModel()
                };
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ApiResult<int> UpdateDep(DEPModel model)
        {
            try
            {
                //根据id查询是否有这条数据没有就修改不了
                var query = _Dep.GetValue(x => x.DepId == model.DepId);
                if (query == null)
                {
                    return new ApiResult<int>()
                    {
                        Code = ApiEnums.Fail,
                        Message = "修改失败，未找到指定的部门信息",
                        Data = 0,
                        TotalCount = 0
                    };
                }

                //保存添加人和添加时间，添加修改人和修改时间
                _Dep.Update(model);

                return new ApiResult<int>()
                {
                    Code = ApiEnums.Success,
                    Message = "修改成功",
                    Data = 1,
                    TotalCount = 0
                };
            }
            catch (Exception ex)
            {
                // 记录异常信息
                Console.WriteLine($"修改部门时发生异常: {ex.Message}");

                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Message = $"修改失败: {ex.Message}",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 修改启用
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ApiResult<int> UpdateEnable(DEPModel model)
        {
            var IsEnable = model.IsEnable;
            //根据id修改逻辑删除为 false
            model = _Dep.GetValue(x => x.DepId == model.DepId);
            model.IsEnable = IsEnable;
            var Message = string.Empty;
            if (IsEnable)
            {
                Message = "启用成功";
            }
            else
            {
                Message = "停用成功";
            }
            return new ApiResult<int>()
            {
                Code = ApiEnums.Success,
                Message = Message,
                Data = _Dep.Update(model),
                TotalCount = 0
            };

        }

        /// <summary>
        /// 软删除（修改状态）
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ApiResult<int> UpdateDelete(string Id)
        {
            //根据id修改逻辑删除为 false
            var model = _Dep.GetValue(x => x.DepId == Id);
            model.IsDeleted = true;
            return new ApiResult<int>()
            {
                Code = ApiEnums.Success,
                Message = "删除成功",
                Data = _Dep.Delete(model),
                TotalCount = 0
            };
        }

        /// <summary>
        /// 查询表是否有该编码
        /// </summary>
        /// <param name="DepId"></param>
        /// <returns></returns>
        public ApiResult<int> GetDepId(string DepId)
        {
            var model = _Dep.GetValue(x => x.DepId == DepId);
            return new ApiResult<int>()
            {
                Code = ApiEnums.Success,
                Message = "成功",
                Data = 1,
                TotalCount = 0
            };

        }
    }
}
